package payrechargelogic

import (
	"context"
	"errors"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelDeleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeChannelDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelDeleteLogic {
	return &RechargeChannelDeleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RechargeChannelDeleteLogic) RechargeChannelDelete(in *rpc.RechargeChannelDeleteRequest) (*rpc.RechargeChannelDeleteResponse, error) {
	var (
		commonResp rpc.CommonResp
		err        error
	)

	if len(in.Id) == 0 {
		commonResp.Code = xerrors.ParameterError
		commonResp.Message = "Invalid parameter"
		return &rpc.RechargeChannelDeleteResponse{CommonResp: &commonResp}, errors.New("invalid Parameter")
	}
	if len(in.Id) > 1 {
		err = model.NewRechargeChannelModel(l.ctx, l.svcCtx.Db).DeleteRechargeChannels(in.Id)
	} else {
		err = model.NewRechargeChannelModel(l.ctx, l.svcCtx.Db).DeleteRechargeChannel(in.Id[0])
	}

	if err != nil {
		commonResp.Code = xerrors.DBError
		commonResp.Message = "Database operation error"
		return &rpc.RechargeChannelDeleteResponse{CommonResp: &commonResp}, err
	}

	commonResp.Message = "Delete successful"
	return &rpc.RechargeChannelDeleteResponse{CommonResp: &commonResp}, nil
}
